import {createRouter, createWebHistory, useRouter} from 'vue-router'

// 开启历史模式
// vue2中使用的mode：history 实现

const routerHistory = createWebHistory();

const router = createRouter({
    history: routerHistory,
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: () => import('/@/views/login/index.vue')
        },
        {
            path: '/home',
            redirect: '/excellent',
            component: () => import('../views/home/Home.vue'),
            children: [
                {
                    path: '/excellent',
                    component: () => import ('../views/excellentPerson/index.vue')
                },
                {
                    path: '/results',
                    component: () => import ('../views/results/index.vue')
                },
                {
                    path: '/results/add',
                    component: () => import ('../views/results/add/index.vue')
                },
                {
                    path: '/excellent/add',
                    component: () => import ('../views/excellentPerson/add/index.vue')
                },
            ]
        },
    ]
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login')
        return next();
    const tokenStr = window.sessionStorage.getItem('token');
    if (!tokenStr)
        return next('/login');
    next();
});

export default router
